﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using BoxSharp;

namespace BoxSharpCore
{
    public class BoxSharpCore : AddonInstance
    {
        private AddonInterface myInterface;
        private ToolTip tip;
        private System.Windows.Media.SolidColorBrush boxBack = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(243, 245, 250));

        public override void AddonMain(AddonInterface addonInterface)
        {
            myInterface = addonInterface;

            myInterface.AddonInitializing += new AddonEventHandler(myInterface_AddonInitializing);
            myInterface.AddonFinalizing += new AddonEventHandler(myInterface_AddonFinalizing);
        }

        void myInterface_AddonFinalizing()
        {
            string config = Path.Combine(myInterface.Path, "config.txt");

            StreamWriter writer = new StreamWriter(config);

            writer.WriteLine("Width=" + myInterface.Box.Width.ToString());
            writer.WriteLine("FontSize=" + myInterface.Box.FontSize.ToString());
            writer.WriteLine("Left=" + myInterface.MainWindow.Left.ToString());
            writer.WriteLine("Top=" + myInterface.MainWindow.Top.ToString());

            writer.Close();
        }

        void myInterface_AddonInitializing()
        {
            myInterface.MainPanel.Margin = new Thickness(2, 1, 2, 1);
            myInterface.Box.Background = boxBack;
            myInterface.Box.Cursor = System.Windows.Input.Cursors.Arrow;

            tip = new ToolTip();
            tip.Content = "/exit\r\nThis command will shut down BoxSharp immediately.";
            tip.HasDropShadow = true;
            tip.PlacementTarget = myInterface.Box;
            tip.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
            tip.IsOpen = false;
            tip.Padding = new Thickness(5);
            tip.VerticalOffset = 5;

            string config = Path.Combine(myInterface.Path, "config.txt");

            double boxWidth = 250;
            double boxFontSize = 12;
            double windowLeft = 500;
            double windowTop = 0;

            if (File.Exists(config))
            {
                StreamReader reader = new StreamReader(config);
                while (reader.Peek() > -1)
                {
                    string line = reader.ReadLine().ToLower();

                    if (line.StartsWith("width="))
                    {
                        boxWidth = Convert.ToDouble(line.Substring(6));
                    }
                    else if (line.StartsWith("fontsize="))
                    {
                        boxFontSize = Convert.ToDouble(line.Substring(9));
                    }
                    else if (line.StartsWith("left="))
                    {
                        windowLeft = Convert.ToDouble(line.Substring(5));
                    }
                    else if (line.StartsWith("top="))
                    {
                        windowTop = Convert.ToDouble(line.Substring(4));
                    }
                }
                reader.Close();
            }

            myInterface.Box.Width = boxWidth;
            myInterface.Box.FontSize = boxFontSize;
            myInterface.MainWindow.Left = windowLeft;
            myInterface.MainWindow.Top = windowTop;

            myInterface.OnCommand += new CommandEventHandler(myInterface_OnCommand);
            myInterface.Box.TextChanged += new TextChangedEventHandler(Box_TextChanged);
            myInterface.Box.MouseEnter += new System.Windows.Input.MouseEventHandler(Box_MouseEnter);
            myInterface.Box.MouseLeave += new System.Windows.Input.MouseEventHandler(Box_MouseLeave);
            myInterface.Box.GotKeyboardFocus += new System.Windows.Input.KeyboardFocusChangedEventHandler(Box_GotKeyboardFocus);
            myInterface.Box.LostKeyboardFocus += new System.Windows.Input.KeyboardFocusChangedEventHandler(Box_LostKeyboardFocus);
        }

        void myInterface_OnCommand(string bscommand)
        {
            if (bscommand.ToLower() == "/exit")
            {
                myInterface.MainWindow.Close();
            }
        }

        void Box_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
        {
            myInterface.Box.Foreground = System.Windows.Media.Brushes.Black;
            myInterface.Box.Background = System.Windows.Media.Brushes.White;
            myInterface.Box.Cursor = System.Windows.Input.Cursors.IBeam;
        }

        void Box_LostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
        {
            myInterface.Box.Foreground = System.Windows.Media.Brushes.Gray;
            myInterface.Box.Background = boxBack;
            myInterface.Box.Cursor = System.Windows.Input.Cursors.Arrow;
        }

        void Box_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!myInterface.Box.IsKeyboardFocused)
            {
                myInterface.Box.Foreground = System.Windows.Media.Brushes.Gray;
                myInterface.Box.Background = boxBack;
            }
        }

        void Box_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            myInterface.Box.Foreground = System.Windows.Media.Brushes.Black;
            myInterface.Box.Background = System.Windows.Media.Brushes.White;
        }

        void Box_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (myInterface.Box.Text.ToLower() == "/exit")
            {
                tip.IsOpen = true;
            }
            else
            {
                tip.IsOpen = false;
            }
        }
    }
}
